package com.csap.framework.oauth2.server.web;

import cn.dev33.satoken.LoginUtil;
import com.csap.framework.annotation.ResponseAdvice;
import com.csap.framework.annotation.apidoc.Api;
import com.csap.framework.annotation.apidoc.ApiOperation;
import com.csap.framework.core.factory.Transform;
import com.csap.framework.extension.result.ResultParam;
import com.csap.framework.extension.result.ResultUtil;
import com.csap.framework.oauth2.open.RoleOpenApi;
import com.csap.framework.oauth2.request.RolesRequest;
import com.csap.framework.oauth2.request.RolesResponse;
import com.csap.framework.oauth2.request.UserPermissionsVo;
import com.csap.framework.oauth2.request.UserRolesRequest;
import com.csap.framework.oauth2.server.model.RolesModel;
import com.csap.framework.oauth2.server.service.RolesService;
import com.csap.framework.oauth2.server.service.UserOpenInfoService;
import com.csap.framework.oauth2.server.service.UserPermissionsService;
import com.csap.framework.oauth2.server.service.UserRolesService;
import com.csap.framework.oauth2.server.service.admin.AdminUserRoleService;
import com.csap.framework.oauth2.server.service.model.UserPermissionsDTO;
import com.csap.framework.util.Assert;
import com.csap.mybatisplus.page.Page;
import com.google.common.collect.Sets;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 角色
 * </p>
 *
 * @author yangchengfu
 * @dateTime 2021-07-25 23:51:30
 */
@RequestMapping("oauth2")
@ResponseAdvice
@Api(description = "角色", value = "角色")
@RestController
public class RolesController implements RoleOpenApi, Transform<Object, Object> {
    @Resource
    private RolesService rolesService;
    @Resource
    private UserPermissionsService permissionsService;
    @Resource
    private UserRolesService userRolesService;
    @Resource
    private UserOpenInfoService openInfoService;
    @Resource
    private AdminUserRoleService adminUserRoleService;

    @ApiOperation("添加或者修改用户权限")
    @Override
    public ResultParam<Boolean> saveOrUpdateUserPermissions(UserPermissionsVo vo) {
        return permissionsService.saveOrUpdate(UserPermissionsDTO.builder()
                .userId(openInfoService.getUserId(LoginUtil.clientId(), vo.getOpenId()))
                .permissions(vo.getPermissions())
                .build()).map(ResultUtil::success).get();
    }

    @ApiOperation("添加角色")
    @Override
    public ResultParam<Long> addRole(RolesRequest rolesModel) {
        return ResultUtil.success(rolesService.insertRole(transform(rolesModel, RolesModel.class)));
    }

    @ApiOperation("修改角色")
    @Override
    public ResultParam<Boolean> updateRole(RolesRequest rolesModel) {
        return ResultUtil.success(rolesService.update(transform(rolesModel, RolesModel.class)));
    }

    @Override
    @ApiOperation("删除角色")
    public ResultParam<Boolean> remove(@RequestParam Long id) {
        return ResultUtil.success(rolesService.delete(id));
    }

    @ApiOperation("分页查询角色")
    @Override
    public ResultParam<Page<RolesResponse>> queryPage(RolesRequest rolesModel) {
        return ResultUtil.success(transform(rolesService.page(rolesModel, rolesService.getPage() == null ? Page.build(rolesModel.getCurrent(), rolesModel.getSize()) : rolesService.getPage()), RolesResponse.class));
    }

    @Override
    public ResultParam<String> queryUserRoleNameByOpenId(UserRolesRequest userRolesRequest) {
        return ResultUtil.success(userRolesService.roleNameList(userRolesRequest.getOpenId(), openInfoService.getUserId(
                Assert.notEmpty(userRolesRequest.getClient_id(), "客户端ID不能为空"),
                Assert.notEmpty(userRolesRequest.getOpenId(), "用户的openId不能为空"))));
    }

    @Override
    @ApiOperation("根据openId查询用户的角色ID")
    public ResultParam<Set<Long>> queryUserRolesByOpenId(UserRolesRequest userRolesRequest) {
        return ResultUtil.success(Sets.newHashSet(userRolesService.roleList(openInfoService.getUserId(
                        Assert.notEmpty(userRolesRequest.getClient_id(), "客户端ID不能为空"),
                        Assert.notEmpty(userRolesRequest.getOpenId(), "用户的openId不能为空")),
                userRolesRequest.getOpenId())));
    }

    @Override
    public ResultParam<Map<Long, RolesResponse>> findMapByIds(String ids) {
        return ResultUtil.success(rolesService.findMapByIds(ids));
    }

    @Override
    @ApiOperation("添加/修改用户角色")
    public ResultParam<Boolean> addUserRoles(@RequestBody UserRolesRequest userRolesRequest) {
        return ResultUtil.success(adminUserRoleService.addUserRoles(userRolesRequest, openInfoService.getUserId(userRolesRequest.getClient_id(), userRolesRequest.getOpenId())));
    }
}
